<template>
  <div class="address">
    <div class="address-heading">收货地址</div>
    <div class="address-context">
      <div class="address-group">
        <router-link class="address-a" to="/member/addAddress">
          <i class="el-icon-plus"></i>
          添加收获地址
        </router-link>
        <ul class="receiver-list el-row">
          <li class="el-col-8" v-for="add in addresses" :key="add.id">
            <div class="list-text">
              <h5 class="text-overflow" :title="add.consignee">
                {{ add.consignee }}
                <span v-show="add.default" class="text-label">默认</span>
              </h5>
              <p class="text-gray-dark">{{ add.phone }}</p>
              <p class="text-overflow" :title="add.areaName + add.address">
                {{ add.areaName }}&nbsp;{{ add.address }}
              </p>
              <p class="text-gray-dark">{{ add.zipCode }}</p>
              <div class="action">
                <router-link
                  class="btn el-icon-edit"
                  :to="{
                    path: 'detailsAddress/:addressId',
                    query: {
                      addressId: add.id,
                    },
                  }"
                  title="编辑"
                ></router-link>
                <button
                  class="btn el-icon-close"
                  @click="deleteAddress(add.id)"
                  title="删除"
                ></button>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from "vue";
import { showaddress, deleteaddress } from "@/api/address";
import router from '@/router'
export default {
  name: "Address",
  setup() {
    let addresses = ref({});

    function getAddress() {
      showaddress().then((res) => {
        addresses.value = res.data.data;
      });
    }
    getAddress();

    function deleteAddress(id) {
      if (confirm("您确定要删除吗？")) {
        let params = {
          id: id.c[0] + "" + id.c[1],
        };
        console.log("删除该地址", params.id);
        deleteaddress(params.id).then((res) => {
          console.log("删除该地址1", params);
          console.log(res);
          router.go(0);
        });
      } else {
        console.log("误点删除按键，请返回！");
      }
    }

    return {
      addresses,
      deleteAddress,
    };
  },
};
</script>

<style scoped>
* {
  font-size: 12px;
  color: #333;
}
a {
  text-decoration: none;
  color: #666666;
}
ul {
  list-style: none;
  padding-left: 0;
}
p {
  margin: 0;
  line-height: 24px;
}
.address {
  background-color: white;
}
.address-heading {
  padding: 10px 15px;
  border-bottom: 1px solid #f0f0f0;
}
.address-context {
  padding: 15px;
  border-bottom: 1px solid #f0f0f0;
}
.address-group {
  width: 100%;
}
.address-a {
  height: 120px;
  line-height: 120px;
  display: block;
  margin: 0 auto;
  color: #999999;
  font-size: 16px;
  text-align: center;
  transition: all 0.3s;
  border-radius: 4px;
  border: 1px dashed #d8d8d8;
}
.address-a:hover,
.address-a:hover i {
  color: #409eff;
  border-color: #409eff;
}
.receiver-list {
  height: 100%;
}
.list-text {
  height: 120px;
  padding: 5px 15px;
  margin-top: 15px;
  margin-right: 15px;
  position: relative;
  transition: border-color 0.3s;
  border-radius: 4px;
  border: 1px solid #e8e8e8;
}
.list-text:hover {
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.08);
  border-color: #dddddd;
}
.list-text:hover .action {
  display: block;
}
.list-text h5 {
  font-size: 14px;
  font-weight: normal;
  line-height: 30px;
  margin-top: 0;
  margin-bottom: 5px;
  border-bottom: 1px dotted #eeeeee;
}
.text-overflow {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.text-gray-dark {
  color: #919191;
  font-size: 12px;
}
.text-label {
  background-color: #409eff;
  color: white;
  margin-left: 10px;
  padding: 3px 8px 4px;
  font-size: 75%;
  font-weight: bold;
  text-align: center;
  vertical-align: baseline;
  border-radius: 2px;
}
.action {
  display: none;
  position: absolute;
  right: 10px;
  bottom: 10px;
}
.btn {
  font-size: 14px;
  padding: 3px 6px;
  margin-right: 5px;
  color: #666666;
  background-color: white;
  transition: all 0.3s;
  border: 1px #d8d8d8 solid;
  border-radius: 4px;
}
.btn:hover {
  color: #409eff;
  border-color: #409eff;
}
</style>